

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.*;
public class CChannelDemo2 {
	public static void main(String[] args) throws IOException, InterruptedException{
		SocketChannel sc = SocketChannel.open();
		sc.connect(new InetSocketAddress("127.0.0.1",9999));
		System.out.println("正在连接服务器");
		while(!sc.finishConnect()){
			Thread.sleep(10);
			System.out.println("thread sleep");
		}

		String str = "java caonima";
		StringBuffer sb = new StringBuffer();
		sb.append(str.length()+"\r\n");
		sb.append(str);
		String sendStr = sb.toString();


		ByteBuffer buf = ByteBuffer.wrap(sendStr.getBytes());
		while(buf.hasRemaining()){
			sc.write(buf);
			System.out.println("写入ing...");
		}

		System.out.println("写入成功");


	}


}